{% extends 'base/base.html' %}

{% block content %}
<div class="row">
    <div class="col-md-4">
        <div class="card mb-4">
            <div class="card-header text-center"
                 style="background-color: {{ tag.color }}20; border-left: 4px solid {{ tag.color }};">
                <h3 class="mb-0">
                    <span class="badge" style="background-color: {{ tag.color }}; color: white; font-size: 1.2em;">
                        {{ tag.name }}
                    </span>
                </h3>
            </div>
            <div class="card-body text-center">
                {% if tag.description %}
                <p class="card-text">{{ tag.description }}</p>
                {% else %}
                <p class="card-text text-muted">暂无描述</p>
                {% endif %}

                <div class="mt-4">
                    <div class="row text-center">
                        <div class="col-6">
                            <div class="display-6 text-primary">{{ tag.food_count }}</div>
                            <small class="text-muted">关联食物</small>
                        </div>
                        <div class="col-6">
                            <div class="display-6 text-success">{{ tag.record_count }}</div>
                            <small class="text-muted">使用记录</small>
                        </div>
                    </div>
                </div>

                <div class="mt-4">
                    {% if tag.parent %}
                    <small class="text-muted">
                        <i class="fas fa-sitemap me-1"></i>
                        父级标签: <span class="badge" style="background-color: {{ tag.parent.color }};">{{ tag.parent.name }}</span>
                    </small>
                    <br>
                    {% endif %}
                    {% if tag.children.all %}
                    <small class="text-muted">
                        <i class="fas fa-sitemap me-1"></i>
                        子级标签: 
                        {% for child in tag.children.all %}
                        <span class="badge me-1" style="background-color: {{ child.color }};">{{ child.name }}</span>
                        {% endfor %}
                    </small>
                    <br>
                    {% endif %}
                    <small class="text-muted">
                        <i class="fas fa-user me-1"></i>
                        创建者: {{ tag.created_by.username|default:"系统" }}
                    </small>
                    <br>
                    <small class="text-muted">
                        <i class="fas fa-calendar-plus me-1"></i>
                        创建时间: {{ tag.created_at|date:"Y年m月d日" }}
                    </small>
                </div>

                <div class="mt-4">
                    <a href="{% url 'main:tag_edit' tag.id %}" class="btn btn-warning btn-sm">
                        <i class="fas fa-edit me-1"></i>编辑标签
                    </a>
                    <a href="{% url 'main:tag_list' %}" class="btn btn-secondary btn-sm">
                        <i class="fas fa-arrow-left me-1"></i>返回列表
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-8">
        <!-- 关联的食物 -->
        <div class="card mb-4">
            <div class="card-header bg-info text-white">
                <h5 class="mb-0">
                    <i class="fas fa-utensils me-2"></i>
                    关联的食物 ({{ foods_with_tag.count }})
                </h5>
            </div>
            <div class="card-body">
                {% if foods_with_tag %}
                <div class="row">
                    {% for food in foods_with_tag %}
                    <div class="col-md-6 mb-3">
                        <div class="card h-100">
                            <div class="card-body">
                                <h6 class="card-title">{{ food.name }}</h6>
                                <p class="card-text text-muted small">{{ food.category.name }}</p>
                                <div class="mt-2">
                                    {% for tag in food.tags.all %}
                                    <span class="badge me-1" style="background-color: {{ tag.color }};">
                                        {{ tag.name }}
                                    </span>
                                    {% endfor %}
                                </div>
                            </div>
                            <div class="card-footer bg-transparent">
                                <a href="{% url 'food_library' %}?food={{ food.id }}"
                                   class="btn btn-sm btn-outline-primary">查看详情</a>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <div class="text-center py-4">
                    <i class="fas fa-utensils fa-3x text-muted mb-3"></i>
                    <p class="text-muted">暂无关联食物</p>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- 使用记录 -->
        <div class="card">
            <div class="card-header bg-warning text-dark">
                <h5 class="mb-0">
                    <i class="fas fa-history me-2"></i>
                    使用记录 ({{ records_with_tag.count }})
                </h5>
            </div>
            <div class="card-body">
                {% if records_with_tag %}
                <div class="list-group">
                    {% for record in records_with_tag %}
                    <div class="list-group-item">
                        <div class="d-flex w-100 justify-content-between">
                            <h6 class="mb-1">{{ record.date|date:"Y年m月d日" }}</h6>
                            <small>{{ record.date|timesince }}前</small>
                        </div>
                        <div class="mb-2">
                            {% if record.breakfast %}
                            <span class="badge bg-info me-1">早餐: {{ record.breakfast.name }}</span>
                            {% endif %}
                            {% if record.lunch %}
                            <span class="badge bg-success me-1">午餐: {{ record.lunch.name }}</span>
                            {% endif %}
                            {% if record.dinner %}
                            <span class="badge bg-warning me-1">晚餐: {{ record.dinner.name }}</span>
                            {% endif %}
                        </div>
                        <div>
                            {% for tag in record.tags.all %}
                            <span class="badge me-1" style="background-color: {{ tag.color }};">
                                {{ tag.name }}
                            </span>
                            {% endfor %}
                        </div>
                    </div>
                    {% endfor %}
                </div>
                {% else %}
                <div class="text-center py-4">
                    <i class="fas fa-history fa-3x text-muted mb-3"></i>
                    <p class="text-muted">暂无使用记录</p>
                </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}